package cn.edu.scau.cmi.hombio.service;

import cn.edu.scau.cmi.hombio.dao.FeedDAO;
import cn.edu.scau.cmi.hombio.dao.InspectionExceptionDAO;
import cn.edu.scau.cmi.hombio.dao.PigHouseDAO;
import cn.edu.scau.cmi.hombio.dao.PigstyDAO;
import cn.edu.scau.cmi.hombio.dao.PreventionDAO;
import cn.edu.scau.cmi.hombio.dao.TaskPigstyDAO;
import cn.edu.scau.cmi.hombio.dao.TransferDAO;

import cn.edu.scau.cmi.hombio.domain.Feed;
import cn.edu.scau.cmi.hombio.domain.InspectionException;
import cn.edu.scau.cmi.hombio.domain.PigHouse;
import cn.edu.scau.cmi.hombio.domain.Pigsty;
import cn.edu.scau.cmi.hombio.domain.Prevention;
import cn.edu.scau.cmi.hombio.domain.TaskPigsty;
import cn.edu.scau.cmi.hombio.domain.Transfer;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for Pigsty entities
 * 
 */

@Service("PigstyService")

@Transactional
public class PigstyServiceImpl implements PigstyService {

	/**
	 * DAO injected by Spring that manages Feed entities
	 * 
	 */
	@Autowired
	private FeedDAO feedDAO;

	/**
	 * DAO injected by Spring that manages InspectionException entities
	 * 
	 */
	@Autowired
	private InspectionExceptionDAO inspectionExceptionDAO;

	/**
	 * DAO injected by Spring that manages PigHouse entities
	 * 
	 */
	@Autowired
	private PigHouseDAO pigHouseDAO;

	/**
	 * DAO injected by Spring that manages Pigsty entities
	 * 
	 */
	@Autowired
	private PigstyDAO pigstyDAO;

	/**
	 * DAO injected by Spring that manages Prevention entities
	 * 
	 */
	@Autowired
	private PreventionDAO preventionDAO;

	/**
	 * DAO injected by Spring that manages TaskPigsty entities
	 * 
	 */
	@Autowired
	private TaskPigstyDAO taskPigstyDAO;

	/**
	 * DAO injected by Spring that manages Transfer entities
	 * 
	 */
	@Autowired
	private TransferDAO transferDAO;

	/**
	 * Instantiates a new PigstyServiceImpl.
	 *
	 */
	public PigstyServiceImpl() {
	}

	/**
	 * Save an existing PigHouse entity
	 * 
	 */
	@Transactional
	public Pigsty savePigstyPigHouse(Integer id, PigHouse related_pighouse) {
		Pigsty pigsty = pigstyDAO.findPigstyByPrimaryKey(id, -1, -1);
		PigHouse existingpigHouse = pigHouseDAO.findPigHouseByPrimaryKey(related_pighouse.getId());

		// copy into the existing record to preserve existing relationships
		if (existingpigHouse != null) {
			existingpigHouse.setId(related_pighouse.getId());
			existingpigHouse.setNumber(related_pighouse.getNumber());
			existingpigHouse.setRfid(related_pighouse.getRfid());
			existingpigHouse.setMemo(related_pighouse.getMemo());
			related_pighouse = existingpigHouse;
		}

		pigsty.setPigHouse(related_pighouse);
		related_pighouse.getPigsties().add(pigsty);
		pigsty = pigstyDAO.store(pigsty);
		pigstyDAO.flush();

		related_pighouse = pigHouseDAO.store(related_pighouse);
		pigHouseDAO.flush();

		return pigsty;
	}

	/**
	 * Delete an existing InspectionException entity
	 * 
	 */
	@Transactional
	public Pigsty deletePigstyInspectionExceptions(Integer pigsty_id, Integer related_inspectionexceptions_id) {
		InspectionException related_inspectionexceptions = inspectionExceptionDAO.findInspectionExceptionByPrimaryKey(related_inspectionexceptions_id, -1, -1);

		Pigsty pigsty = pigstyDAO.findPigstyByPrimaryKey(pigsty_id, -1, -1);

		related_inspectionexceptions.setPigsty(null);
		pigsty.getInspectionExceptions().remove(related_inspectionexceptions);

		inspectionExceptionDAO.remove(related_inspectionexceptions);
		inspectionExceptionDAO.flush();

		return pigsty;
	}

	/**
	 * Save an existing Feed entity
	 * 
	 */
	@Transactional
	public Pigsty savePigstyFeeds(Integer id, Feed related_feeds) {
		Pigsty pigsty = pigstyDAO.findPigstyByPrimaryKey(id, -1, -1);
		Feed existingfeeds = feedDAO.findFeedByPrimaryKey(related_feeds.getId());

		// copy into the existing record to preserve existing relationships
		if (existingfeeds != null) {
			existingfeeds.setId(related_feeds.getId());
			existingfeeds.setTime(related_feeds.getTime());
			existingfeeds.setMemo(related_feeds.getMemo());
			related_feeds = existingfeeds;
		}

		related_feeds.setPigsty(pigsty);
		pigsty.getFeeds().add(related_feeds);
		related_feeds = feedDAO.store(related_feeds);
		feedDAO.flush();

		pigsty = pigstyDAO.store(pigsty);
		pigstyDAO.flush();

		return pigsty;
	}

	/**
	 * Delete an existing Prevention entity
	 * 
	 */
	@Transactional
	public Pigsty deletePigstyPreventions(Integer pigsty_id, Integer related_preventions_id) {
		Prevention related_preventions = preventionDAO.findPreventionByPrimaryKey(related_preventions_id, -1, -1);

		Pigsty pigsty = pigstyDAO.findPigstyByPrimaryKey(pigsty_id, -1, -1);

		related_preventions.setPigsty(null);
		pigsty.getPreventions().remove(related_preventions);

		preventionDAO.remove(related_preventions);
		preventionDAO.flush();

		return pigsty;
	}

	/**
	 * Delete an existing Feed entity
	 * 
	 */
	@Transactional
	public Pigsty deletePigstyFeeds(Integer pigsty_id, Integer related_feeds_id) {
		Feed related_feeds = feedDAO.findFeedByPrimaryKey(related_feeds_id, -1, -1);

		Pigsty pigsty = pigstyDAO.findPigstyByPrimaryKey(pigsty_id, -1, -1);

		related_feeds.setPigsty(null);
		pigsty.getFeeds().remove(related_feeds);

		feedDAO.remove(related_feeds);
		feedDAO.flush();

		return pigsty;
	}

	/**
	 * Save an existing TaskPigsty entity
	 * 
	 */
	@Transactional
	public Pigsty savePigstyTaskPigsties(Integer id, TaskPigsty related_taskpigsties) {
		Pigsty pigsty = pigstyDAO.findPigstyByPrimaryKey(id, -1, -1);
		TaskPigsty existingtaskPigsties = taskPigstyDAO.findTaskPigstyByPrimaryKey(related_taskpigsties.getId());

		// copy into the existing record to preserve existing relationships
		if (existingtaskPigsties != null) {
			existingtaskPigsties.setId(related_taskpigsties.getId());
			existingtaskPigsties.setMemo(related_taskpigsties.getMemo());
			related_taskpigsties = existingtaskPigsties;
		}

		related_taskpigsties.setPigsty(pigsty);
		pigsty.getTaskPigsties().add(related_taskpigsties);
		related_taskpigsties = taskPigstyDAO.store(related_taskpigsties);
		taskPigstyDAO.flush();

		pigsty = pigstyDAO.store(pigsty);
		pigstyDAO.flush();

		return pigsty;
	}

	/**
	 */
	@Transactional
	public Pigsty findPigstyByPrimaryKey(Integer id) {
		return pigstyDAO.findPigstyByPrimaryKey(id);
	}

	/**
	 * Delete an existing TaskPigsty entity
	 * 
	 */
	@Transactional
	public Pigsty deletePigstyTaskPigsties(Integer pigsty_id, Integer related_taskpigsties_id) {
		TaskPigsty related_taskpigsties = taskPigstyDAO.findTaskPigstyByPrimaryKey(related_taskpigsties_id, -1, -1);

		Pigsty pigsty = pigstyDAO.findPigstyByPrimaryKey(pigsty_id, -1, -1);

		related_taskpigsties.setPigsty(null);
		pigsty.getTaskPigsties().remove(related_taskpigsties);

		taskPigstyDAO.remove(related_taskpigsties);
		taskPigstyDAO.flush();

		return pigsty;
	}

	/**
	 * Delete an existing PigHouse entity
	 * 
	 */
	@Transactional
	public Pigsty deletePigstyPigHouse(Integer pigsty_id, Integer related_pighouse_id) {
		Pigsty pigsty = pigstyDAO.findPigstyByPrimaryKey(pigsty_id, -1, -1);
		PigHouse related_pighouse = pigHouseDAO.findPigHouseByPrimaryKey(related_pighouse_id, -1, -1);

		pigsty.setPigHouse(null);
		related_pighouse.getPigsties().remove(pigsty);
		pigsty = pigstyDAO.store(pigsty);
		pigstyDAO.flush();

		related_pighouse = pigHouseDAO.store(related_pighouse);
		pigHouseDAO.flush();

		pigHouseDAO.remove(related_pighouse);
		pigHouseDAO.flush();

		return pigsty;
	}

	/**
	 * Load an existing Pigsty entity
	 * 
	 */
	@Transactional
	public Set<Pigsty> loadPigstys() {
		return pigstyDAO.findAllPigstys();
	}

	/**
	 * Save an existing Pigsty entity
	 * 
	 */
	@Transactional
	public void savePigsty(Pigsty pigsty) {
		Pigsty existingPigsty = pigstyDAO.findPigstyByPrimaryKey(pigsty.getId());

		if (existingPigsty != null) {
			if (existingPigsty != pigsty) {
				existingPigsty.setId(pigsty.getId());
				existingPigsty.setNumber(pigsty.getNumber());
				existingPigsty.setRfid(pigsty.getRfid());
				existingPigsty.setCapacity(pigsty.getCapacity());
				existingPigsty.setMemo(pigsty.getMemo());
			}
			pigsty = pigstyDAO.store(existingPigsty);
		} else {
			pigsty = pigstyDAO.store(pigsty);
		}
		pigstyDAO.flush();
	}

	/**
	 * Save an existing Prevention entity
	 * 
	 */
	@Transactional
	public Pigsty savePigstyPreventions(Integer id, Prevention related_preventions) {
		Pigsty pigsty = pigstyDAO.findPigstyByPrimaryKey(id, -1, -1);
		Prevention existingpreventions = preventionDAO.findPreventionByPrimaryKey(related_preventions.getId());

		// copy into the existing record to preserve existing relationships
		if (existingpreventions != null) {
			existingpreventions.setId(related_preventions.getId());
			existingpreventions.setTime(related_preventions.getTime());
			existingpreventions.setMemo(related_preventions.getMemo());
			related_preventions = existingpreventions;
		}

		related_preventions.setPigsty(pigsty);
		pigsty.getPreventions().add(related_preventions);
		related_preventions = preventionDAO.store(related_preventions);
		preventionDAO.flush();

		pigsty = pigstyDAO.store(pigsty);
		pigstyDAO.flush();

		return pigsty;
	}

	/**
	 * Delete an existing Pigsty entity
	 * 
	 */
	@Transactional
	public void deletePigsty(Pigsty pigsty) {
		pigstyDAO.remove(pigsty);
		pigstyDAO.flush();
	}

	/**
	 * Return all Pigsty entity
	 * 
	 */
	@Transactional
	public List<Pigsty> findAllPigstys(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<Pigsty>(pigstyDAO.findAllPigstys(startResult, maxRows));
	}

	/**
	 * Save an existing Transfer entity
	 * 
	 */
	@Transactional
	public Pigsty savePigstyTransfers(Integer id, Transfer related_transfers) {
		Pigsty pigsty = pigstyDAO.findPigstyByPrimaryKey(id, -1, -1);
		Transfer existingtransfers = transferDAO.findTransferByPrimaryKey(related_transfers.getId());

		// copy into the existing record to preserve existing relationships
		if (existingtransfers != null) {
			existingtransfers.setId(related_transfers.getId());
			existingtransfers.setTime(related_transfers.getTime());
			existingtransfers.setMemo(related_transfers.getMemo());
			related_transfers = existingtransfers;
		}

		related_transfers.setPigsty(pigsty);
		pigsty.getTransfers().add(related_transfers);
		related_transfers = transferDAO.store(related_transfers);
		transferDAO.flush();

		pigsty = pigstyDAO.store(pigsty);
		pigstyDAO.flush();

		return pigsty;
	}

	/**
	 * Return a count of all Pigsty entity
	 * 
	 */
	@Transactional
	public Integer countPigstys() {
		return ((Long) pigstyDAO.createQuerySingleResult("select count(o) from Pigsty o").getSingleResult()).intValue();
	}

	/**
	 * Delete an existing Transfer entity
	 * 
	 */
	@Transactional
	public Pigsty deletePigstyTransfers(Integer pigsty_id, Integer related_transfers_id) {
		Transfer related_transfers = transferDAO.findTransferByPrimaryKey(related_transfers_id, -1, -1);

		Pigsty pigsty = pigstyDAO.findPigstyByPrimaryKey(pigsty_id, -1, -1);

		related_transfers.setPigsty(null);
		pigsty.getTransfers().remove(related_transfers);

		transferDAO.remove(related_transfers);
		transferDAO.flush();

		return pigsty;
	}

	/**
	 * Save an existing InspectionException entity
	 * 
	 */
	@Transactional
	public Pigsty savePigstyInspectionExceptions(Integer id, InspectionException related_inspectionexceptions) {
		Pigsty pigsty = pigstyDAO.findPigstyByPrimaryKey(id, -1, -1);
		InspectionException existinginspectionExceptions = inspectionExceptionDAO.findInspectionExceptionByPrimaryKey(related_inspectionexceptions.getId());

		// copy into the existing record to preserve existing relationships
		if (existinginspectionExceptions != null) {
			existinginspectionExceptions.setId(related_inspectionexceptions.getId());
			existinginspectionExceptions.setMemo(related_inspectionexceptions.getMemo());
			related_inspectionexceptions = existinginspectionExceptions;
		}

		related_inspectionexceptions.setPigsty(pigsty);
		pigsty.getInspectionExceptions().add(related_inspectionexceptions);
		related_inspectionexceptions = inspectionExceptionDAO.store(related_inspectionexceptions);
		inspectionExceptionDAO.flush();

		pigsty = pigstyDAO.store(pigsty);
		pigstyDAO.flush();

		return pigsty;
	}
}
